/**
 * 
 */
package org.mspring.mlog.service.impl;

import java.util.List;

import org.mspring.mlog.dao.CatalogDao;
import org.mspring.mlog.entity.Catalog;
import org.mspring.mlog.entity.Link;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.service.CatalogService;
import org.mspring.platform.core.AbstractServiceSupport;
import org.mspring.platform.persistence.query.QueryCriterion;
import org.mspring.platform.persistence.support.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author fairchildy
 * @since 2012-7-18
 * @Description
 * @TODO
 */
@Service
@Transactional
public class CatalogServiceImpl extends AbstractServiceSupport implements CatalogService {

	@Autowired
	private CatalogDao catalogDao;


    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.CatalogService#deleteCatalog(java.lang.Long[])
     */
    @Override
    public void deleteCatalog(Long... idArray) {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.CatalogService#findCatalog(org.mspring.platform
     * .persistence.support.Page, java.lang.String, java.lang.Object[])
     */
    @Override
    public Page<Catalog> findCatalog(Page<Catalog> page, String queryString, Object... params) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.CatalogService#findCatalog(org.mspring.platform
     * .persistence.support.Page, java.lang.String)
     */
    @Override
    public Page<Catalog> findCatalog(Page<Catalog> page, String queryString) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.CatalogService#findCatalog(org.mspring.platform
     * .persistence.support.Page,
     * org.mspring.platform.persistence.query.QueryCriterion)
     */
    @Override
    public Page<Catalog> findCatalog(Page<Catalog> page, QueryCriterion criterion) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.CatalogService#getCatalogById(java.lang.Long)
     */
    @Override
    public Catalog getCatalogById(Long catalogId) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.CatalogService#findAllCatalog()
     */
    @Override
    public List<Catalog> findAllCatalog() {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.CatalogService#getCatalogByName(java.lang.String
     * )
     */
    @Override
    public Catalog getCatalogByName(String name) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.CatalogService#catalogExists(java.lang.String,
     * java.lang.String)
     */
    @Override
    public boolean catalogExists(String name, Long id) {
        // TODO Auto-generated method stub
            return true;
    }

	@Override
	public Page<Catalog> findCatalog(Page<Catalog> catalogPage) {
		
		int pageFrom = (catalogPage.getPageNo() - 1)* catalogPage.getPageSize();
		int pageCount = 0;
		Catalog link = new Catalog();
		link.setPageFrom(pageFrom);
		link.setuSER_ID(catalogPage.getuSER_ID());
		if (catalogPage.getTotalCount() < 0) {
			pageCount = catalogDao.getPageCount(link);
			catalogPage.setTotalCount(pageCount);
			catalogPage.setTotalPages(pageCount/catalogPage.getPageSize() + 1);
		}
		
		List<Catalog> list = catalogDao.getPage(link);
		catalogPage.setResult(list);
		
		return catalogPage;
	}

	@Override
	public void deleteCatalog(Catalog catalog) {
		catalogDao.deleteCatalog(catalog);
		
	}

	@Override
	public void createCatalog(Catalog catalog) {
		catalogDao.createCatalog(catalog);
	}

	@Override
	public Catalog getCatalogById(Catalog catalog) {
		// TODO Auto-generated method stub
		return catalogDao.getCatalogById(catalog);
	}

	@Override
	public void updateCatalog(Catalog catalog) {
		catalogDao.updateCatalog(catalog);
	}

	@Override
	public boolean catalogExists(Catalog catalog) {
		List<Catalog> result = catalogDao.catalogExists(catalog);
		if (null == result || result.size() == 0) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public List<Catalog> findAllCatalog(Post post) {
		// TODO Auto-generated method stub
		return catalogDao.findAllCatalog(post);
	}

}
